package com.bharatonjava.hospital.web;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bharatonjava.hospital.dto.SearchDto;
import com.bharatonjava.hospital.service.IMiscService;

@Controller
@RequestMapping(value = "/search")
public class SearchController {

	private final Logger log = Logger.getLogger(this.getClass());

	private IMiscService miscService;

	@Autowired
	public void setMiscService(IMiscService miscService) {
		this.miscService = miscService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView search(@RequestParam("q") String query) {

		log.info("search - " + query);
		ModelAndView mav = new ModelAndView();
		mav.addObject("q", query);
		mav.setViewName("searchResults");

		List<SearchDto> lst = miscService.search(query);

		mav.addObject("searchResult", lst);
		return mav;
	}
}
